package org.dromara.fms.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.web.core.BaseService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.fms.domain.vo.FmsAccountVo;
import org.dromara.fms.domain.bo.FmsAccountBo;
import org.dromara.fms.service.impl.FmsAccountServiceImpl;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 账户信息
 *
 * @author wenrui Su
 * @date 2024-03-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/fms/account")
public class FmsAccountController extends BaseService {

    private final FmsAccountServiceImpl fmsAccountService;

    /**
     * 查询账户信息列表
     */
    @SaCheckPermission("fms:account:list")
    @GetMapping("/list")
    public TableDataInfo<FmsAccountVo> list(FmsAccountBo bo, PageQuery pageQuery) {
        return fmsAccountService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出账户信息列表
     */
    @SaCheckPermission("fms:account:export")
    @Log(title = "账户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(FmsAccountBo bo, HttpServletResponse response) {
        List<FmsAccountVo> list = fmsAccountService.queryList(bo);
        ExcelUtil.exportExcel(list, "账户信息", FmsAccountVo.class, response);
    }

    /**
     * 获取账户信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("fms:account:query")
    @GetMapping("/{id}")
    public R<FmsAccountVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(fmsAccountService.queryById(id));
    }

    /**
     * 新增账户信息
     */
    @SaCheckPermission("fms:account:add")
    @Log(title = "账户信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody FmsAccountBo bo) {
        return toAjax(fmsAccountService.insertByBo(bo));
    }

    /**
     * 修改账户信息
     */
    @SaCheckPermission("fms:account:edit")
    @Log(title = "账户信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody FmsAccountBo bo) {
        return toAjax(fmsAccountService.updateByBo(bo));
    }

    /**
     * 删除账户信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("fms:account:remove")
    @Log(title = "账户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(fmsAccountService.deleteWithValidByIds(List.of(ids), true));
    }
}
